package com.ouc.pampas;

import javax.servlet.http.HttpServletRequest;

public class IpUtil
{
  private static final String[] HEADERS = { "x-forwarded-for", "HEADER_X_FORWARDED_FOR", "Proxy-Client-IP", "WL-Proxy-Client-IP" };
  
  public static String getIpAddress(HttpServletRequest request)
  {
    for (String header : HEADERS)
    {
      String ip = request.getHeader(header);
      if (isValidIp(ip)) {
        return ip;
      }
    }
    return request.getRemoteAddr();
  }
  
  private static boolean isValidIp(String ip)
  {
    if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
      return false;
    }
    return true;
  }
}
